package handler

import (
	"log"
	pay "git.oschina.net/fanbuchi/xgggh/pay/proto/pay"
	"golang.org/x/net/context"
	"git.oschina.net/fanbuchi/xgggh/pay/service"
	"git.oschina.net/fanbuchi/xgggh/pay/service/mmpay/protocol"
	"encoding/xml"
	"git.oschina.net/fanbuchi/xgggh/pay/model"
)

type Pay struct{
	service.Pay
}

//Create micro query com.xgggh.srv.pay Pays.Create '{"order": {"id":308357643048157185}, "payWay": 2001}'
/*{
"ok": true,
"params": {
"partnerId": "1401969102",
"appid": "wx20171222091426be64381d300989970622",
"prepayid": "wx20171222091426be64381d300989970622",
"package": "Sign=WXPay",
"noncestr": "ZndpYF4SDxXFCwul",
"timestamp": "1513905266",
"sign": "D5746CDDA56BC814E949A8DC0E6C7B9B"
}
}*/
func (handler *Pay) Create(ctx context.Context, req *pay.CreateReq, rsp *pay.CreateRsp) error {
	// oauth2Service
	payRsp,err:=handler.Pay.Create(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
		rsp.Params=payRsp
	}
	return nil
}

//Query micro query com.xgggh.srv.pay Pays.Query '{"id": 307201722912112641}'
func (handler *Pay) Query(ctx context.Context, req *pay.QueryReq, rsp *pay.QueryRsp) error {
	// oauth2Service
	order,err:=handler.Pay.Query(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
		rsp.Pay= order
	}
	return nil
}

//支付结果通知 update支付状态
//wx(xml)--->nginx(lua)--json-->micro(handler)--->wx(xml)
//1.协议判断 wx ali
//2.业务判断
//3.签名验证 map
//4.根据支付订单更新状态
//micro query com.xgggh.srv.pay Pays.Notify '{"order": {"id":308438289696456705}, "payWay": 2001}'
func (handler *Pay) Notify(ctx context.Context, req *pay.NotifyReq, rsp *pay.NotifyRsp) error {
	if req.PayWay==pay.PayWay_wxPay {
		notify:=model.WxPayNotifyRsp{}
		err:=handler.Pay.Notify(req)
		if err!=nil {
			notify.ReturnCode=protocol.FAIL
			notify.ReturnMsg=err.Error()
		}else{
			notify.ReturnCode=protocol.SUCCESS
		}
		data,err:=xml.Marshal(notify)
		rsp.Result=string(data)
	}
	return nil
}

// Delete micro query com.xgggh.srv.pay Pays.Delete '{"id": 307203191521509377}'
func (handler *Pay) Delete(ctx context.Context, req *pay.DeleteReq, rsp *pay.Response) error {
	err:=handler.Pay.Delete(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
	}
	return nil
}


//Search micro query com.xgggh.srv.pay Pays.Search '{"page_number":1,"result_per_page":2,"params":{"orderId":307201722912112641}}'
func (handler *Pay) Search(ctx context.Context, req *pay.SearchReq, rsp *pay.SearchRsp) error {
	orders,err:=handler.Pay.Search(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
		rsp.Pay = orders
	}
	return nil
}

// Stream is a server side stream handler called via client.Stream or the generated client code
func (e *Pay) Stream(ctx context.Context, req *pay.StreamingRequest, stream pay.Pays_StreamStream) error {
	log.Printf("Received Example.Stream request with count: %d", req.Count)

	for i := 0; i < int(req.Count); i++ {
		log.Printf("Responding: %d", i)
		if err := stream.Send(&pay.StreamingResponse{
			Count: int64(i),
		}); err != nil {
			return err
		}
	}

	return nil
}

// PingPong is a bidirectional stream handler called via client.Stream or the generated client code
func (e *Pay) PingPong(ctx context.Context, stream pay.Pays_PingPongStream) error {
	for {
		req, err := stream.Recv()
		if err != nil {
			return err
		}
		log.Printf("Got ping %v", req.Stroke)
		if err := stream.Send(&pay.Pong{Stroke: req.Stroke}); err != nil {
			return err
		}
	}
}
